﻿using System;
using System.Collections.Generic;
using System.Linq;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALModificationPropertyValue : DALBase
    {
        public static DALModificationPropertyValue Singleton = new DALModificationPropertyValue();

        public GTModificationPropertyValue Add(GTModificationPropertyValue propertyValue)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var mpv = new ModificationPropertyValue();
                mpv.ModificationId = propertyValue.ModificationId;
                mpv.PropertyId = propertyValue.PropertyId;
                mpv.Value = propertyValue.Value;

                db.ModificationPropertyValues.InsertOnSubmit(mpv);
                db.SubmitChanges();
                mpv.Id = propertyValue.Id;
                return propertyValue;
            }
        }

        public GTModificationPropertyValue Update(GTModificationPropertyValue propertyValue)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var mpv = db.ModificationPropertyValues.SingleOrDefault(x => x.Id == propertyValue.Id);

                mpv.ModificationId = propertyValue.ModificationId;
                mpv.PropertyId = propertyValue.PropertyId;
                mpv.Value = propertyValue.Value;

                db.SubmitChanges();
                return propertyValue;
            }
        }


        public GTModificationPropertyValue Get(int modificationId, int propertyId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.ModificationPropertyValues.SingleOrDefault(x => x.ModificationId == modificationId && x.PropertyId == propertyId);

                if (brand == null)
                    return null;

                GTModificationPropertyValue gtBrand = new GTModificationPropertyValue();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }


        public string GetPropVal(int modificationId, int propertyId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.ModificationPropertyValues.SingleOrDefault(x => x.ModificationId == modificationId && x.PropertyId == propertyId);

                if (brand == null)
                    return null;

                GTModificationPropertyValue gtBrand = new GTModificationPropertyValue();
                gtBrand.UpdateTo(brand);
                return gtBrand.Value;
            }
        }



        public List<SimpleObject> GetbyModelSimple(int modificationId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                List<ModificationPropertyValue> brand = db.ModificationPropertyValues.Where(x => x.ModificationId == modificationId).ToList();

                
                List<SimpleObject> ret = new List<SimpleObject>();
                

                foreach (var modificationPropertyValue in brand)
                {
                    var simpleObject = new SimpleObject();

                    simpleObject.Prop1 = modificationPropertyValue.ModificationId.ToString();
                    simpleObject.Prop2 = modificationPropertyValue.PropertyId.ToString();
                    simpleObject.Prop3 = modificationPropertyValue.Value;

                    ret.Add(simpleObject);
                }
                
                return ret;
            }
        }


        public void Delete(int modificationId, int propertyId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.ModificationPropertyValues.SingleOrDefault(x => x.ModificationId == modificationId && x.PropertyId == propertyId);

                if (singleOrDefault != null)
                {
                    db.ModificationPropertyValues.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }

    }
}
